package server.ppt;

import java.io.File;

import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.ComThread;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;

public class PptCapturer extends Thread {

	private Dispatch presentation;
	private String savaPath;
	private Integer ppSaveAsJPG = 17;
	private ActiveXComponent ppt;
	private String scaledPath;

	public PptCapturer(ActiveXComponent ppt, Dispatch presentation,
			String savaPath, String scaledPath) {
		this.ppt = ppt;
		this.savaPath = savaPath;
		this.scaledPath = scaledPath;
		this.presentation = presentation;
	}

	@Override
	public void run() {
		File dir = new File(this.savaPath);
		if (!dir.exists())
			dir.mkdir();
		try {
			Dispatch.call(presentation, "SaveAs", savaPath, new Variant(
					ppSaveAsJPG));
			new Thread(new PptScaler(savaPath, this.scaledPath)).start();
		} catch (Exception e) {
			e.printStackTrace();
			if (presentation != null) {
				Dispatch.call(presentation, "Close", new Variant(true));
			}
			ppt.invoke("Quit", new Variant[] {});
			ComThread.Release();
			presentation = null;
			ppt = null;
		}
	}
}
